﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using IIControls.Source;

namespace IIControls.IAControl
{
    /// <summary>
    /// AllOper.xaml 的交互逻辑
    /// </summary>
    public partial class AllOper : Window
    {
        public PumpCmd Channel1Open { get; set; }
        public PumpCmd Channel1Close { get; set; }
        public PumpCmd Channel2Open { get; set; }
        public PumpCmd Channel2Close { get; set; }
        public PumpCmd Channel3Open { get; set; }
        public PumpCmd Channel3Close { get; set; }
        public PumpCmd ReadRecord { get; set; }
        public PumpCmd ClearRecord { get; set; }
        public PumpCmd SyncTime { get; set; }
        public PumpCmd ReadTime { get; set; }
        public PumpCmd ValveOpen { get; set; }
        public PumpCmd ValveClose { get; set; }
        public AllOper(PumpClick GPB, AllOperVar AV)
        {
            InitializeComponent();
            IIControls.IAControl.PumpControl.PumpCMD PCMD = AV.CMD;
            if ((PCMD & PumpControl.PumpCMD.Channel1Open )!=0)
                Channel1Open=AddButton(PumpCmd.Cmd.Channel1Open, 0, 0, true);
            else
                Channel1Open=AddButton(PumpCmd.Cmd.Channel1Open, 0, 0, false);
            if ((PCMD & PumpControl.PumpCMD.Channel1Close ) != 0)
                Channel1Close=AddButton( PumpCmd.Cmd.Channel1Close , 0, 1,true );
            else
                Channel1Close= AddButton(PumpCmd.Cmd.Channel1Close, 0, 1, false );
            if ((PCMD & PumpControl.PumpCMD.Channel2Open ) != 0)
                Channel2Open= AddButton( PumpCmd.Cmd.Channel2Open , 0, 2,true );
            else
                Channel2Open=AddButton(PumpCmd.Cmd.Channel2Open, 0, 2, false );
            if ((PCMD & PumpControl.PumpCMD.Channel2Close ) != 0)
                Channel2Close= AddButton( PumpCmd.Cmd.Channel2Close , 0, 3,true );
            else
                Channel2Close= AddButton(PumpCmd.Cmd.Channel2Close, 0, 3, false);
            if ((PCMD & PumpControl.PumpCMD.Channel3Open ) != 0)
                Channel3Open=AddButton( PumpCmd.Cmd.Channel3Open , 0, 4,true );
            else
                Channel3Open=AddButton(PumpCmd.Cmd.Channel3Open, 0, 4, false );
            if ((PCMD & PumpControl.PumpCMD.Channel3Close ) != 0)
                Channel3Close=AddButton( PumpCmd.Cmd.Channel3Close , 0, 5,true );
            else
                Channel3Close=AddButton(PumpCmd.Cmd.Channel3Close, 0, 5, false );
            if ((PCMD & PumpControl.PumpCMD.ReadRecord ) != 0)
                ReadRecord=AddButton( PumpCmd.Cmd.ReadRecord , 1, 0,true );
            else
                ReadRecord=AddButton(PumpCmd.Cmd.ReadRecord, 1, 0, false );
            if ((PCMD & PumpControl.PumpCMD.ClearRecord ) != 0)
                ClearRecord=AddButton( PumpCmd.Cmd.ClearRecord , 1, 1,true );
            else
                ClearRecord=AddButton(PumpCmd.Cmd.ClearRecord, 1, 1, false );
            if ((PCMD & PumpControl.PumpCMD.SyncTime ) != 0)
                SyncTime=AddButton( PumpCmd.Cmd.SyncTime , 1, 2,true );
            else
                SyncTime=AddButton(PumpCmd.Cmd.SyncTime, 1, 2, false );
            if ((PCMD & PumpControl.PumpCMD.ReadTime ) != 0)
                ReadTime=AddButton( PumpCmd.Cmd.ReadTime , 1, 3,true );
            else
                ReadTime= AddButton(PumpCmd.Cmd.ReadTime, 1, 3, false );
            if ((PCMD & PumpControl.PumpCMD.ValveOpen) != 0)
                ValveOpen = AddButton(PumpCmd.Cmd.ValveOpen , 1, 4, true);
            else
                ValveOpen = AddButton(PumpCmd.Cmd.ValveOpen, 1, 4, false);
            if ((PCMD & PumpControl.PumpCMD.ValveClose) != 0)
                ValveClose  = AddButton(PumpCmd.Cmd.ValveClose , 1, 5, true);
            else
                ValveClose = AddButton(PumpCmd.Cmd.ValveClose, 1, 5, false);
            PUMPC = GPB;
            FrontPress = AV.FontPress;
            BackPress = AV.BackPress;
            PumpFlow = AV.PumpFlow;
            if(AV.PumpVoltage != null )
                PumpVoltage = AV.PumpVoltage;
            if (AV.PumpElectric != null)
                PumpElectric = AV.PumpElectric;
        }

        private PumpCmd AddButton(IIControls.Source.PumpCmd.Cmd ButtonType, int gridrow, int gridcol,bool Isenable)
        {
            PumpCmd PC = new Source.PumpCmd(ButtonType,new Source.PumpCmd.GetPumpButton(PClick));
            Grid.SetColumn(PC, gridcol);
            Grid.SetRow(PC, gridrow);
            PC.Margin = new Thickness(3, 1.5, 3, 1.5);
            PC.IsEnable = Isenable;
            DrawButton.Children.Add(PC);
            return PC;
        }

        private void  AddButton(IIControls.Source.PumpCmd.Cmd ButtonType,int gridrow,int gridcol, PumpCmd.GetPumpButton P)
        {
            PumpCmd PC=  new Source.PumpCmd(ButtonType, P);
            Grid.SetColumn(PC, gridcol);
            Grid.SetRow(PC, gridrow);
            PC.Margin = new Thickness(3, 1.5, 3, 1.5);
            DrawButton.Children.Add(PC);         
        }


      public   PumpClick PUMPC;
      public   delegate void PumpClick(PumpCmd PC);


        void PClick(PumpCmd PC)
        {
            PUMPC(PC);
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Cursor = Cursors.Hand;
            this.DragMove();
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void ExitDoor_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }


        private void Mouse_Enter(object sender, MouseEventArgs e)
        {
            Path   Ui = (Path )sender;
            Ui.Cursor  = Cursors.Hand;
        }

        private void Mouse_Leave(object sender, MouseEventArgs e)
        {
            Path Ui = (Path)sender;
            Ui.Cursor = Cursors.Arrow ;
        }

        private double  frontpress;
        /// <summary>
        /// 前端压力
        /// </summary>
        public double  FrontPress
        {
            get { return frontpress; }
            set
            {
                frontpress = value;
                txt_FrontPress.Text  = Math.Round(frontpress, 2).ToString("0.00");

                if (frontpress >= 100)
                    txt_FrontPress.Text = ((int)frontpress).ToString();
                else if (frontpress >= 10 && frontpress < 100)
                    txt_FrontPress.Text = Math.Round(frontpress, 2).ToString("0.0");
                else if (frontpress >= 0 && frontpress < 1)
                    txt_FrontPress.Text = Math.Round(frontpress, 2).ToString("0.00");
            }
        }


        private double backpress;
        /// <summary>
        /// 后端压力
        /// </summary>
        public double BackPress
        {
            get { return backpress; }
            set { backpress = value;
                txt_BackPress.Text = ((int)backpress).ToString();
                if (backpress >= 100)
                    txt_BackPress.Text = ((int)backpress).ToString();
                else if (backpress >= 10 && backpress < 100)
                    txt_BackPress.Text = Math.Round(backpress, 2).ToString("0.0");
                else if (backpress >= 0 && backpress < 1)
                    txt_BackPress.Text = Math.Round(backpress, 2).ToString("0.00");
            }
        }


        private double pumpflow;
        /// <summary>
        /// 泵流量
        /// </summary>
        public double PumpFlow
        {
            get { return pumpflow; }
            set {
                pumpflow = value;
                if(pumpflow>=100)
                txt_PumpFlow.Text = ((int)pumpflow).ToString();
                else if(pumpflow>=10 && pumpflow<100)
                    txt_PumpFlow.Text = Math.Round(pumpflow, 2).ToString("0.0");
                else if(pumpflow>=0 && pumpflow<1)
                    txt_PumpFlow.Text = Math.Round(pumpflow, 2).ToString("0.00");
            }
        }


        private double[] pumpvoltage;
        /// <summary>
        /// 三相电电压
        /// </summary>
        public double[] PumpVoltage
        {
            get { return pumpvoltage; }
            set
            {
                pumpvoltage = value;
                if ( pumpvoltage==null || pumpvoltage.Length != 3)
                    return;
                UA.Text =" "+ Math.Round(pumpvoltage[0], 2).ToString("0.00")+" V";
                UB.Text = " " + Math.Round(pumpvoltage[1], 2).ToString("0.00") + " V";
                UC.Text = " " + Math.Round(pumpvoltage[2], 2).ToString("0.00") + " V";
            }
        }


        private double[] pumpelectric;
        /// <summary>
        /// 三相电电流
        /// </summary>
        public double[] PumpElectric
        {
            get { return pumpelectric; }
            set
            {
                pumpelectric = value;
                if (pumpelectric==null || pumpelectric.Length != 3)
                    return;
                IA.Text = " " + Math.Round(pumpelectric[0], 2).ToString("0.00") + " A";
                IB.Text = " " + Math.Round(pumpelectric[1], 2).ToString("0.00") + " A";
                IC.Text = " " + Math.Round(pumpelectric[2], 2).ToString("0.00") + " A";
            }
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void whiteboard_MouseEnter(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void whiteboard_MouseLeave(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Arrow ;
        }
    }
}
